package dnl.persistence;

import java.util.*;

public class TableInfo {

	private String tableName;
	private Map<String, ColumnInfo> columnInfos = new HashMap<>();

	public TableInfo(String tableName) {
		this.tableName = tableName;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public void addColumnInfo(String name, SqlType sqlType) {
		columnInfos.put(name, new ColumnInfo(name, sqlType));
	}

    public void addColumnInfo(String name, SqlType sqlType, int ordinal) {
        final ColumnInfo columnInfo = new ColumnInfo(name, sqlType);
        columnInfo.setOrdinal(ordinal);
        columnInfos.put(name, columnInfo);
	}
	
	public void addColumnInfo(String name) {
		columnInfos.put(name, new ColumnInfo(name, SqlType.VARCHAR, columnInfos.size()));
	}

	public List<ColumnInfo> getColumnInfos(){
        final ArrayList<ColumnInfo> columnInfosList = new ArrayList<>(columnInfos.values());
        Collections.sort(columnInfosList, new Comparator<ColumnInfo>() {
            @Override
            public int compare(ColumnInfo o1, ColumnInfo o2) {
                Integer i1 = o1.getOrdinal();
                Integer i2 = o2.getOrdinal();
                return i1.compareTo(i2);
            }
        });
        return columnInfosList;
    }

	public ColumnInfo[] getColumnInfosAsArray(){
		List<ColumnInfo> columnInfosList = getColumnInfos();
        return columnInfosList.toArray(new ColumnInfo[columnInfosList.size()]);
	}
	
	public String[] getColumnNames() {
		ColumnInfo[] columnInfosAsArray = getColumnInfosAsArray();
		String[] columnNames = new String[columnInfosAsArray.length];
		for (int i = 0; i < columnInfosAsArray.length; i++) {
			columnNames[i] = columnInfosAsArray[i].getColumnName();
		}
		return columnNames;
	}
	
}
